package chixing.day12;

import chixing.day12.exception.BalanceNotEnoughException;

public class TryDemo {
    public static void main(String[] args) {
//        try {
//            Class.forName("chixing.day11.clone_demo.Student");
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }

        fun3();

    }

    public static void fun1() {
        CreditAccount creditAccount1 = new CreditAccount();
        creditAccount1.setAccBalance(200);
        try {//1.捕捉异常(可能会出现异常)
            creditAccount1.withdraw(300);//取款
            System.out.println("try 捕捉异常");
        } catch (BalanceNotEnoughException e) {//2.处理异常
            e.printStackTrace();
            System.out.println("catch 处理异常");
        }
    }

    public static void fun2() {
        try {
            String s = null;
            s.substring(3);
            int a = 10 / 0;
        } catch (NullPointerException e) {
            System.out.println("处理NullPointerException异常的代码");
        } catch (ArithmeticException e) {
            System.out.println("处理ArithmeticException异常代码");
        } catch (RuntimeException e) {
            System.out.println("处理RuntimeException异常的代码");
        } catch (Exception e) {
            System.out.println("处理Exception异常");
        }
    }

    public static void fun3() {
        try {
            System.out.println("try开始捕捉异常");
            int a = 10 / 0;//有异常后面代码不执行
            System.out.println("try 捕捉异常结束");
        } catch (Exception e) {
            System.out.println("catch 处理异常");
        } finally {//【业务上：释放资源、节省内存，如IO流关闭，数据库连接关闭】无论是否有异常，都执行
            System.out.println("final");
        }
        System.out.println("33333333333");
    }

    public static int fun4() {
        int a = 10;
        int b = 10;
        try {
            int c = a / b;
            return 11;
        } catch (Exception e) {
            System.out.println("--------catch---------");
            return 22;
        } finally {
            System.out.println("final");
        }
    }


}
